package thread;

import util.PrestoJDBCTool;
import util.SQLConstant;
import util.SQLConstant2;

import java.sql.SQLException;
import java.util.Vector;

/**
 * @author Xiahu
 * @create 2023/8/4 0004
 */
public class AuthorNameDisambiguationThread implements Runnable {
    private Vector<String> vector;
    private String flag;
    private String authorName;

    private PrestoJDBCTool jdbcTool;

    public AuthorNameDisambiguationThread(Vector<String> vector, String flag, String authorName) {
        this.vector = vector;
        this.flag = flag;
        this.authorName = authorName;
        this.jdbcTool = new PrestoJDBCTool();
    }

    @Override
    public void run() {
        String sql = null;


        System.out.println(authorName);
        System.out.println("取出: " + flag);
        //2、删除 paper_author_name_tmp、patent_author_name_tmp 数据
        try {
            jdbcTool.connect();

            jdbcTool.statement.execute(String.format(SQLConstant2.deletePaperTmp, flag));

            jdbcTool.statement.execute(String.format(SQLConstant2.deletePatentTmp, flag));

            //3、根据人名,查询paper_author_name 生成 paper_author_name_tmp 数据
            jdbcTool.statement.execute(String.format(SQLConstant2.queryForPaperTmp, flag, authorName));

            //4、根据人名,查询patent_author_name 生成 patent_author_name_tmp 数据
            jdbcTool.statement.execute(String.format(SQLConstant2.queryForPatentTmp, flag, authorName));

            //解析数据,请求接口,返回json,落地数据
            sql = String.format(SQLConstant2.accessInterfaceSQL, flag, flag, flag, flag, flag, flag, flag, flag, flag);
            jdbcTool.statement.execute(sql);
        } catch (Exception e) {
            System.out.println(sql);
            throw new RuntimeException(e);
        } finally {
            vector.add(flag);
            System.out.println("添加: " + flag);
            try {
                jdbcTool.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
